<template>
  <div class="homepage-index-wrap">
    <Header/>
    <Swiper :banner="banner" :height="height"/>
    <FreeWater @click.native="toFree"/>
    <ProductList/>
    <Menu active="1"/>

    <ServiceArea v-if="showAddress == 1" type="homePage" @closeArea="handleClose"/>
  </div>
</template>

<script>
import api from "../../api";
import common from "../../api/common";
import Header from "@/components/homepage/Header";
import Swiper from "@/components/homepage/Swiper";
import FreeWater from "@/components/homepage/FreeWater";
import ProductList from "@/components/homepage/ProductList";
import Menu from "@/components/common/Menu";
import ServiceArea from "@/components/common/ServiceArea";

export default {
  title: "首页",
  components: { Header, Swiper, FreeWater, ProductList, Menu, ServiceArea },
  data() {
    return {
      height: 200,
      banner: [],
      showAddress: this.$store.state.showArea
    };
  },
  methods: {
    setGolbalDeposit() {
      api.usercenter.getDeposit(res => {
        this.$store.commit("setDeposit", res.money);
      });
    },
    initIndex() {
      api.homepage.index(1, res => {
        this.banner = res.banner;
      });
    },
    handleClose() {
      this.showAddress = 2;
      this.$store.commit("setShowArea");
    },
    saveUserInfo() {
      // 保存用户uid 和 invite_code
      let uid = localStorage.getItem("uid");
      let invite_code = localStorage.getItem("invite_code");
      this.$store.commit("setUid", uid);
      this.$store.commit("setInviteCode", invite_code);
    },
    toFree() {
      // 跳转到免费喝水页面
      var invite_code = this.$store.state.invite_code;
      //分享只需要带着invite_code
      var params = `?invite_code=${invite_code}&inType=invite`;
      var path = "/pages/free/free" + params;
      wx.miniProgram.navigateTo({ url: path });
    },

    getUseTicket() {
      // 使用水票
      let isuseTicket = localStorage.getItem("isuseTicket");
      let productId = localStorage.getItem("productId");
      let useTicketNumber = localStorage.getItem("useTicketNumber");

      if (isuseTicket == "useTicket") {
        this.$routerPush(
          this,
          `/pages/address/Index?mode=select&productId=${productId}&number=${useTicketNumber}`
        );
        // 清空记录
        localStorage.setItem("isuseTicket", "");
      } else {
        return;
      }
    },
    toPaySuccess() {
      // 跳转到支付成功
      let payType = localStorage.getItem("payType");
      if (payType == "ticket" || payType == "deposit") {
        this.$routerPush(this, `/pages/deposit/Success?payType=${payType}`);
        localStorage.setItem("payType", "");
      }
    },
    toShareRegister() {
      // 分享跳转进来的
      let inType = localStorage.getItem("inType");
      if (inType == "invite") {
        //定义path 与小程序的支付页面的路径相对应
        var path = "/pages/register/register" + params;
        wx.miniProgram.navigateTo({ url: path });
        localStorage.setItem("inType", "");
      }
    },
    toApply() {
      // 申请入股
      let applyType = localStorage.getItem("applyType");
      let shareCode = localStorage.getItem("shareCode");
      if (applyType == "applyType" ) {
        this.$routerPush(this, `/pages/profit/Apply`);
        localStorage.setItem("payType", "");
      }
      if (applyType == "applyType" && shareCode ) {
        this.$routerPush(this, `/pages/profit/Apply?shareCode=${shareCode}`);
        localStorage.setItem("payType", "");
        localStorage.setItem("shareCode", "");
      }
    }
  },
  created() {
    this.saveUserInfo();
  },
  mounted() {
    setTimeout(() => {
      this.setGolbalDeposit();
      this.initIndex();
      this.$toast.clear();
      // 支付成功回调
      this.toPaySuccess();
      // 使用水票回调
      this.getUseTicket();
      // 分享注册
      this.toShareRegister();
      // 重新申请
      this.toApply()
    }, 1 * 100);

    if (this.showArea == 1) {
    }
  }
};
</script>

<style lang="less" scoped>
.homepage-index-wrap {
}
</style>